package org.javaymw.springboot.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.javaymw.springboot.dto.UserRoleDto;
import org.javaymw.springboot.entity.UserRoleEntity;
import org.javaymw.springboot.mapper.UserRoleMapper;
import org.javaymw.springboot.service.UserRoleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: zhaoxinguo
 * @Date: 2018/7/26 12:49
 * @Description:
 */
@Service
@Transactional
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRoleEntity> implements UserRoleService {

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Override
    public Integer save(UserRoleDto userRoleDto) {
        UserRoleEntity userRoleEntity = new UserRoleEntity();
        BeanUtils.copyProperties(userRoleDto, userRoleEntity);
        return userRoleMapper.insert(userRoleEntity);
    }

    @Override
    public List<Long> getRoleIdsByUserId(Long id) {
        Wrapper<UserRoleEntity> wrapper = new EntityWrapper<>();
        wrapper.eq("user_id", id);
        List<UserRoleEntity> userRoleEntities = userRoleMapper.selectList(wrapper);
        if (userRoleEntities == null) {
            return null;
        }
        List<Long> roleIds = new ArrayList<>(userRoleEntities.size());
        userRoleEntities.stream().forEach(userRoleEntity -> {
            roleIds.add(userRoleEntity.getRoleId());
        });
        return roleIds;
    }

    @Override
    public void deleteOrSave(Long userId, List<Long> roleIds) {
        // 1.删除当前用户对应的历史角色
        Wrapper<UserRoleEntity> wrapper = new EntityWrapper<>();
        wrapper.eq("user_id", userId);
        List<UserRoleEntity> userRoleEntities = userRoleMapper.selectList(wrapper);
        if (userRoleEntities != null) {
            userRoleEntities.stream().forEach(userRoleEntity -> {
                userRoleMapper.deleteById(userRoleEntity.getId());
            });
        }
        // 2.保存当前用户对应的新角色
        if (roleIds != null) {
            roleIds.stream().forEach(roleId -> {
                UserRoleEntity bizEntity = new UserRoleEntity();
                bizEntity.setUserId(userId);
                bizEntity.setRoleId(roleId);
                userRoleMapper.insert(bizEntity);
            });
        }
    }

    @Override
    public void deleteUserRoleByUserId(Long userId) {
        Wrapper<UserRoleEntity> wrapper = new EntityWrapper<>();
        wrapper.eq("user_id", userId);
        List<UserRoleEntity> userRoleEntities = userRoleMapper.selectList(wrapper);
        if (userRoleEntities != null) {
            userRoleEntities.stream().forEach(userRoleEntity -> {
                userRoleMapper.deleteById(userRoleEntity.getId());
            });
        }
    }

    @Override
    public Integer getRoleIdsByRoleId(Long roleId) {
        Wrapper<UserRoleEntity> wrapper = new EntityWrapper<>();
        wrapper.eq("role_id", roleId);
        return userRoleMapper.selectCount(wrapper);
    }

    @Override
    public List<Long> getUserIdsByRoleId(Long roleId) {
        Wrapper<UserRoleEntity> wrapper = new EntityWrapper<>();
        wrapper.eq("role_id", roleId);
        List<UserRoleEntity> userRoleEntities = userRoleMapper.selectList(wrapper);
        if (userRoleEntities == null) {
            return null;
        }
        List<Long> userIds = new ArrayList<>(userRoleEntities.size());
        userRoleEntities.stream().forEach(userRoleEntity -> {
            userIds.add(userRoleEntity.getUserId());
        });
        return userIds;
    }

    @Override
    public List<UserRoleDto> list() {
        List<UserRoleEntity> userRoleEntities = userRoleMapper.selectList(new EntityWrapper<>());
        if (CollectionUtils.isEmpty(userRoleEntities)) {
            return null;
        }
        List<UserRoleDto> userRoleDtos = new ArrayList<>(userRoleEntities.size());
        userRoleEntities.forEach(userRoleEntity -> {
            UserRoleDto userRoleDto = new UserRoleDto();
            BeanUtils.copyProperties(userRoleEntity, userRoleDto);
            userRoleDtos.add(userRoleDto);
        });
        return userRoleDtos;
    }
}
